<template>
  <el-dialog :title="showTitle" :visible="showDialog" @close="btnCancel">
    <!-- 表单 -->
    <el-form
      ref="deptForm"
      :model="deptForm"
      :rules="deptRules"
      label-width="120px"
    >
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="deptForm.name"
          size="mini"
          placeholder="2-10个字符"
          class="ipt80"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="deptForm.code"
          size="mini"
          placeholder="2-10个字符"
          class="ipt80"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select v-model="deptForm.managerId" class="ipt80" size="mini">
          <!-- label显示 -->
          <el-option
            v-for="item in managerList"
            :key="item.id"
            :value="item.id"
            :label="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="deptForm.introduce"
          placeholder="1-100个字符"
          type="textarea"
          size="mini"
          :rows="4"
          style="width: 80%"
        />
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button size="mini" @click="btnCancel">取 消</el-button>
            <el-button
              size="mini"
              type="primary"
              @click="btnOk"
            >确 定</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import {
  addDepartment,
  getDepartment,
  getManagerList,
  getDepartmentDetail,
  updateDepartment
} from '@/api/department'
export default {
  name: 'AddDept',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      managerList: [],

      deptForm: {
        name: '',
        code: '',
        introduce: '',
        managerId: '',
        pid: ''
      },
      deptRules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          {
            min: 2,
            max: 10,
            message: '部门名称长度为2-10位',
            trigger: 'blur'
          },
          {
            trigger: 'blur',
            validator: async(rule, value, callback) => {
              let result = await getDepartment()
              // 区分环境
              if (this.deptForm.id) {
                // 编辑场景排除自身
                result = result.filter((item) => item.id !== this.deptForm.id)
              }
              if (result.some((item) => item.name === value)) {
                callback(new Error('部门名称已存在'))
              } else {
                callback()
              }
            }
          }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          {
            min: 2,
            max: 10,
            message: '部门编码长度为2-10位',
            trigger: 'blur'
          },
          {
            trigger: 'blur',
            validator: async(rule, value, callback) => {
              let result = await getDepartment()
              // 区分环境
              if (this.deptForm.id) {
                // 编辑场景排除自身
                result = result.filter((item) => item.id !== this.deptForm.id)
              }
              if (result.some((item) => item.code === value)) {
                callback(new Error('部门编码已存在'))
              } else {
                callback()
              }
            }
          }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 100,
            message: '部门介绍内容长度为1-100位',
            trigger: 'blur'
          }
        ],
        managerId: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    showTitle() {
      return this.deptForm.id ? '编辑部门' : '新增部门'
    }
  },
  created() {
    // 一般封装方法 不直接调用接口
    this.getManagerList()
  },

  methods: {
    btnCancel() {
      // 手动重置

      this.deptForm = {
        name: '',
        code: '',
        introduce: '',
        managerId: '',
        pid: ''
      }
      this.$refs.deptForm.resetFields() // 只能重置表单里绑定的数据

      this.$emit('update:showDialog', false)
    },
    btnOk() {
      // 表单总体检验
      this.$refs.deptForm.validate(async(isOk) => {
        if (isOk) {
          let msg = '新增部门'
          if (this.deptForm.id) {
            // 编辑部门
            msg = '修改部门'
            await updateDepartment(this.deptForm)
          } else {
            // 新增
            await addDepartment({ ...this.deptForm, pid: this.currentId })
          }
          this.$message.success(`${msg}成功`)
          this.$emit('updateDepartment')
          this.btnCancel()
        }
      })
    },
    async getManagerList() {
      // 调用接口
      this.managerList = await getManagerList()
      // console.log(this.managerList);
    },
    // 子组件封装方法 父组件调用
    async getDepartmentDetail(id) {
      this.deptForm = await getDepartmentDetail(id)
    }
  }
}
</script>

<style scoped>
.ipt80 {
  width: 80%;
}
</style>
